home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / Color Picker SDK / Sample Code / Picker Demo Sample / TestPicker.c < prev    next >
Encoding:
Text File  |  1997-06-13  |  11.7 KB  |  470 lines  |  [TEXT/CWIE]

  1.  
  2. //===============================================================================
  3. //===============================================================================
  4. //
  5. //                                    Picker  Demo
  6. //
  7. //                                  john calhoun 1997
  8. //
  9. //===============================================================================
  10. //===============================================================================
  11.  
  12.  
  13. #include "TestPicker.h"
  14.  
  15.  
  16. pascal Boolean SimpleEventFilter (EventRecord *);
  17. pascal void SimpleColorFilter (long, PMColorPtr);
  18. pascal void NSimpleColorFilter (long, NPMColorPtr);
  19. void main (void);
  20.  
  21.  
  22. PickerCWRec        gSyncWorld;
  23. CMColor            cmykColor;
  24. RgnHandle        wholeVenn, regionR, regionG, regionB, blackRgn;
  25. RgnHandle        regionRG, regionGB, regionBR, regionRGB;
  26. Rect            windowBounds, rectC, rectM, rectY, rectK;
  27. RGBColor        theRGBColor;
  28. MenuRef            appleMenu, fileMenu, editMenu, testMenu;
  29. WindowRef        simpleWindow;
  30. Boolean            quitting, memoryAlert, resForkAlert;
  31.  
  32.  
  33. //===================================================================== Functions
  34. //--------------------------------------------------------------------- SimpleGetColor
  35.  
  36. void SimpleGetColor (void)
  37. {
  38.     Point        where;
  39.     Boolean        colorChanged;
  40.     
  41.     SetPt(&where, 0, 0);
  42.     
  43.     colorChanged = GetColor(where, "\pBoring, old GetColor().", &theRGBColor, &theRGBColor);
  44.     if (colorChanged)
  45.     {
  46.         SetPort(simpleWindow);
  47.         InvalRect(&windowBounds);
  48.     }
  49. }
  50.  
  51. //--------------------------------------------------------------------- SimpleEventFilter
  52.  
  53. #if GENERATINGCFM
  54. RoutineDescriptor SimpleEventFilterRD = 
  55.         BUILD_ROUTINE_DESCRIPTOR (uppUserEventProcInfo, SimpleEventFilter);
  56. #endif
  57.  
  58. pascal Boolean SimpleEventFilter (EventRecord *theEvent)
  59. {
  60.     WindowPtr    whichWindow;
  61.     SInt32        menuChoice;
  62.     SInt16        thePart;
  63.     Boolean        handledIt;
  64.     
  65.     handledIt = false;
  66.     
  67.     switch (theEvent->what)
  68.     {
  69.         case mouseDown:
  70.         thePart = FindWindow(theEvent->where, &whichWindow);
  71.         switch (thePart)
  72.         {
  73.             case inSysWindow:
  74.             SystemClick(theEvent, whichWindow);
  75.             break;
  76.             
  77.             case inMenuBar:
  78.             menuChoice = MenuSelect(theEvent->where);
  79.             handledIt = DoMenuChoice(menuChoice);
  80.             break;
  81.         }
  82.         break;
  83.         
  84.         case updateEvt:
  85.         handledIt = DoUpdateEvent(theEvent);
  86.         break;
  87.     }
  88.     
  89.     UpdateBouncingLine();
  90.     
  91.     return (handledIt);
  92. }
  93.  
  94. //--------------------------------------------------------------------- SimpleColorFilter
  95.  
  96. #if GENERATINGCFM
  97. RoutineDescriptor SimpleColorFilterRD = 
  98.         BUILD_ROUTINE_DESCRIPTOR (uppColorChangedProcInfo, SimpleColorFilter);
  99. #endif
  100.  
  101. pascal void SimpleColorFilter (long userData, PMColorPtr newColor)
  102. {
  103. #pragma unused (userData)
  104.     GrafPtr        wasPort;
  105.     long        csVers;
  106.     CMError        syncError;
  107.     CMWorldRef    syncWorld;
  108.     CMColor        theColor;
  109.     OSErr        theErr;
  110.     
  111.     GetPort(&wasPort);
  112.     SetPort(simpleWindow);
  113.     
  114.     theErr = Gestalt(gestaltColorMatchingVersion, &csVers);
  115.     if ((newColor->profile) && (theErr == noErr))
  116.     {
  117.         syncError = CWNewColorWorld(&syncWorld, newColor->profile, 0L);
  118.         if ((syncError == noErr) || (syncError == cmProfilesIdentical))
  119.         {
  120.             theColor = newColor->color;
  121.             CWMatchColors(syncWorld, &theColor, 1);
  122.             CWDisposeColorWorld(syncWorld);
  123.         }
  124.     }
  125.     else
  126.         theColor.rgb = newColor->color.rgb;
  127.     
  128.     theRGBColor.red = theColor.rgb.red;
  129.     theRGBColor.green = theColor.rgb.green;
  130.     theRGBColor.blue = theColor.rgb.blue;
  131.     
  132.     UpdateVennColors();
  133.     UpdateBouncingLine();
  134.     
  135.     SetPort(wasPort);
  136. }
  137.  
  138. //--------------------------------------------------------------------- NSimpleColorFilter
  139.  
  140. #if GENERATINGCFM
  141. RoutineDescriptor NSimpleColorFilterRD = 
  142.         BUILD_ROUTINE_DESCRIPTOR (uppNColorChangedProcInfo, NSimpleColorFilter);
  143. #endif
  144.  
  145. pascal void NSimpleColorFilter (long userData, NPMColorPtr newColor)
  146. {
  147. #pragma unused (userData)
  148.     GrafPtr        wasPort;
  149.     CMColor        theColor;
  150.     
  151.     theColor = newColor->color;
  152.     
  153.     if (GetProfileColorSpace(newColor->profile) == cmCMYKData)
  154.     {
  155.         cmykColor = newColor->color;
  156.         UpdateCMYKColors();
  157.     }
  158.     
  159.     PickerMatchColors(&gSyncWorld, &theColor, 1);
  160.     
  161.     theRGBColor.red = theColor.rgb.red;
  162.     theRGBColor.green = theColor.rgb.green;
  163.     theRGBColor.blue = theColor.rgb.blue;
  164.     
  165.     GetPort(&wasPort);
  166.     SetPort(simpleWindow);
  167.     
  168.     UpdateVennColors();
  169.     UpdateBouncingLine();
  170.     
  171.     SetPort(wasPort);
  172. }
  173.  
  174. //--------------------------------------------------------------------- PickColorMovableModal
  175.  
  176. void PickColorMovableModal (long pickerType)
  177. {
  178.     ColorPickerInfo    theColorInfo;
  179.     RGBColor        wasColor;
  180.     UserEventUPP    simpleEventFilterUPP;
  181.     ColorChangedUPP    simpleColorFilterUPP;
  182.     OSErr            theErr;
  183.     
  184.     wasColor = theRGBColor;
  185.     
  186. #if GENERATINGCFM
  187.     simpleEventFilterUPP = &SimpleEventFilterRD;
  188.     simpleColorFilterUPP = &SimpleColorFilterRD;
  189. #else
  190.     simpleEventFilterUPP = &SimpleEventFilter;
  191.     simpleColorFilterUPP = &SimpleColorFilter;
  192. #endif
  193.     
  194.     theColorInfo.placeWhere = kCenterOnMainScreen;
  195.     theColorInfo.dialogOrigin.h = 0;
  196.     theColorInfo.dialogOrigin.v = 0;
  197.     
  198.     theColorInfo.mInfo.editMenuID = 130;
  199.     theColorInfo.mInfo.undoItem = 1;
  200.     theColorInfo.mInfo.cutItem = 3;
  201.     theColorInfo.mInfo.copyItem = 4;
  202.     theColorInfo.mInfo.pasteItem = 5;
  203.     theColorInfo.mInfo.clearItem = 6;
  204.     
  205.     theColorInfo.dstProfile = 0L;
  206.     theColorInfo.pickerType = pickerType;
  207.     theColorInfo.flags = DialogIsMoveable | DialogIsModal | CanModifyPalette | 
  208.             CanAnimatePalette | CallColorProcLive;
  209.     theColorInfo.eventProc = simpleEventFilterUPP;
  210.     theColorInfo.colorProc = simpleColorFilterUPP;
  211.     theColorInfo.colorProcData = 0L;
  212.     
  213.     theColorInfo.theColor.profile = 0;
  214.     theColorInfo.theColor.color.rgb.red = theRGBColor.red;
  215.     theColorInfo.theColor.color.rgb.green = theRGBColor.green;
  216.     theColorInfo.theColor.color.rgb.blue = theRGBColor.blue;
  217.     
  218.     if (pickerType == 'HTML')
  219.         PasStringCopy("\pHTML Picker is brought up alone.", theColorInfo.prompt);
  220.     else
  221.         PasStringCopy("\pThis is the fancy PickColor().", theColorInfo.prompt);
  222.     
  223.     theErr = PickColor(&theColorInfo);
  224.     
  225.     if ((theErr == noErr) && (theColorInfo.newColorChosen))
  226.     {
  227.         theRGBColor.red = theColorInfo.theColor.color.rgb.red;
  228.         theRGBColor.green = theColorInfo.theColor.color.rgb.green;
  229.         theRGBColor.blue = theColorInfo.theColor.color.rgb.blue;
  230.         SetPort(simpleWindow);
  231.         InvalRect(&windowBounds);
  232.     }
  233.     else
  234.     {
  235.         theRGBColor = wasColor;
  236.         SetPort(simpleWindow);
  237.         InvalRect(&windowBounds);
  238.     }
  239. }
  240.  
  241. //--------------------------------------------------------------------- NPickColorMovableModal
  242.  
  243. void NPickColorMovableModal (void)
  244. {
  245.     NColorPickerInfo    theColorInfo;
  246.     RGBColor            wasColor;
  247.     UserEventUPP        simpleEventFilterUPP;
  248.     NColorChangedUPP    simpleColorFilterUPP;
  249.     CMProfileRef        destProfile;
  250.     Handle                profileHandle;
  251.     OSErr                theErr;
  252.     
  253.                 // Get SWOP profile
  254.     profileHandle = GetResource('prof', -16002);
  255.     if (!profileHandle)
  256.         goto fail;
  257.     DetachResource(profileHandle);
  258.     theErr = OpenProfileHandle(&destProfile, profileHandle, 0L, 
  259.             cmPerceptual);
  260.     if (theErr)
  261.         goto fail;
  262.     
  263.     wasColor = theRGBColor;
  264.     
  265. #if GENERATINGCFM
  266.     simpleEventFilterUPP = &SimpleEventFilterRD;
  267.     simpleColorFilterUPP = &NSimpleColorFilterRD;
  268. #else
  269.     simpleEventFilterUPP = &SimpleEventFilter;
  270.     simpleColorFilterUPP = &NSimpleColorFilter;
  271. #endif
  272.     
  273.     theColorInfo.placeWhere = kCenterOnMainScreen;
  274.     theColorInfo.dialogOrigin.h = 0;
  275.     theColorInfo.dialogOrigin.v = 0;
  276.     
  277.     theColorInfo.mInfo.editMenuID = 130;
  278.     theColorInfo.mInfo.undoItem = 1;
  279.     theColorInfo.mInfo.cutItem = 3;
  280.     theColorInfo.mInfo.copyItem = 4;
  281.     theColorInfo.mInfo.pasteItem = 5;
  282.     theColorInfo.mInfo.clearItem = 6;
  283.     
  284.     theColorInfo.dstProfile = destProfile;
  285.     theColorInfo.pickerType = 0L;
  286.     theColorInfo.flags = DialogIsMoveable | DialogIsModal | CanModifyPalette | 
  287.             CanAnimatePalette | AppIsColorSyncAware | CallColorProcLive;
  288.     theColorInfo.eventProc = simpleEventFilterUPP;
  289.     theColorInfo.colorProc = simpleColorFilterUPP;
  290.     theColorInfo.colorProcData = 0L;
  291.     
  292.     theColorInfo.theColor.profile = 0L;
  293.     theColorInfo.theColor.color.rgb.red = theRGBColor.red;
  294.     theColorInfo.theColor.color.rgb.green = theRGBColor.green;
  295.     theColorInfo.theColor.color.rgb.blue = theRGBColor.blue;
  296.     
  297.     PasStringCopy("\pSoft proof with a profile.", theColorInfo.prompt);
  298.     
  299.     theErr = NPickerNewColorWorld(&gSyncWorld, 
  300.             theColorInfo.theColor.profile, theColorInfo.dstProfile, 0L, 
  301.             cmRGBData, cmRGBData);
  302.     if (theErr != noErr)
  303.     {
  304.         SysBeep(10);
  305.     }
  306.     
  307.     theErr = NPickColor(&theColorInfo);
  308.     
  309.     if ((theErr == noErr) && (theColorInfo.newColorChosen))
  310.     {
  311.         PickerMatchColors(&gSyncWorld, &(theColorInfo.theColor.color), 1);
  312.         
  313.         theRGBColor.red = theColorInfo.theColor.color.rgb.red;
  314.         theRGBColor.green = theColorInfo.theColor.color.rgb.green;
  315.         theRGBColor.blue = theColorInfo.theColor.color.rgb.blue;
  316.         SetPort(simpleWindow);
  317.         InvalRect(&windowBounds);
  318.     }
  319.     else
  320.     {
  321.         theRGBColor = wasColor;
  322.         SetPort(simpleWindow);
  323.         InvalRect(&windowBounds);
  324.     }
  325.     
  326.     (void) CMCloseProfile(destProfile);
  327.     if (profileHandle)
  328.         DisposeHandle(profileHandle);
  329.     PickerDisposeColorWorld(&gSyncWorld);
  330.     
  331.     return;
  332.     
  333. fail:
  334.     
  335.     SysBeep(10);
  336.     if (profileHandle)
  337.         DisposeHandle(profileHandle);
  338.     
  339.     return;
  340. }
  341.  
  342. //--------------------------------------------------------------------- NPickColorCMYK
  343.  
  344. void NPickColorCMYK (void)
  345. {
  346.     NColorPickerInfo    theColorInfo;
  347.     CMColor                wasColor;
  348.     UserEventUPP        simpleEventFilterUPP;
  349.     NColorChangedUPP    simpleColorFilterUPP;
  350.     CMProfileRef        inOutProfile;
  351.     Handle                profileHandle;
  352.     OSErr                theErr;
  353.     
  354.                 // Get Color LW 12/600 PS profile
  355.     profileHandle = GetResource('prof', -16000);
  356.     if (!profileHandle)
  357.         goto fail;
  358.     DetachResource(profileHandle);
  359.     theErr = OpenProfileHandle(&inOutProfile, profileHandle, 0L, cmPerceptual);
  360.     if (theErr)
  361.         goto fail;
  362.     
  363.     wasColor = cmykColor;
  364.     
  365. #if GENERATINGCFM
  366.     simpleEventFilterUPP = &SimpleEventFilterRD;
  367.     simpleColorFilterUPP = &NSimpleColorFilterRD;
  368. #else
  369.     simpleEventFilterUPP = &SimpleEventFilter;
  370.     simpleColorFilterUPP = &NSimpleColorFilter;
  371. #endif
  372.     
  373.     theColorInfo.placeWhere = kCenterOnMainScreen;
  374.     theColorInfo.dialogOrigin.h = 0;
  375.     theColorInfo.dialogOrigin.v = 0;
  376.     
  377.     theColorInfo.mInfo.editMenuID = 130;
  378.     theColorInfo.mInfo.undoItem = 1;
  379.     theColorInfo.mInfo.cutItem = 3;
  380.     theColorInfo.mInfo.copyItem = 4;
  381.     theColorInfo.mInfo.pasteItem = 5;
  382.     theColorInfo.mInfo.clearItem = 6;
  383.     
  384.     theColorInfo.dstProfile = 0L;
  385.     theColorInfo.pickerType = 'CMYK';
  386.     theColorInfo.flags = DialogIsMoveable | DialogIsModal | CanModifyPalette | 
  387.             CanAnimatePalette | AppIsColorSyncAware | CallColorProcLive;
  388.     theColorInfo.eventProc = simpleEventFilterUPP;
  389.     theColorInfo.colorProc = simpleColorFilterUPP;
  390.     theColorInfo.colorProcData = 0L;
  391.     
  392.     theColorInfo.theColor.profile = inOutProfile;
  393.     theColorInfo.theColor.color = cmykColor;
  394.     
  395.     PasStringCopy("\pPick a color in CMYK space.", theColorInfo.prompt);
  396.     
  397.     theErr = NPickerNewColorWorld(&gSyncWorld, 
  398.             theColorInfo.theColor.profile, theColorInfo.dstProfile, 0L, 
  399.             cmCMYKData, cmRGBData);
  400.     if (theErr)
  401.         goto fail;
  402.     
  403.     theErr = NPickColor(&theColorInfo);
  404.     
  405.     if ((theErr == noErr) && (theColorInfo.newColorChosen))
  406.     {
  407.         cmykColor = theColorInfo.theColor.color;
  408.         
  409.         PickerMatchColors(&gSyncWorld, &(theColorInfo.theColor.color), 1);
  410.         
  411.         theRGBColor.red = theColorInfo.theColor.color.rgb.red;
  412.         theRGBColor.green = theColorInfo.theColor.color.rgb.green;
  413.         theRGBColor.blue = theColorInfo.theColor.color.rgb.blue;
  414.         SetPort(simpleWindow);
  415.         InvalRect(&windowBounds);
  416.     }
  417.     else
  418.     {
  419.         cmykColor = wasColor;
  420.         SetPort(simpleWindow);
  421.         InvalRect(&windowBounds);
  422.     }
  423.     
  424.     (void) CMCloseProfile(inOutProfile);
  425.     DisposeHandle(profileHandle);
  426.     PickerDisposeColorWorld(&gSyncWorld);
  427.     
  428.     return;
  429.     
  430. fail:
  431.     
  432.     SysBeep(10);
  433.     if (profileHandle)
  434.         DisposeHandle(profileHandle);
  435.     return;
  436. }
  437.  
  438. //--------------------------------------------------------------------- main
  439.  
  440. void main (void)
  441. {
  442.     InitToolbox();
  443.     AddMenusToMenuBar();
  444.     OpenSimpleWindow();
  445.     AllocateVennColorDiagramRegions();
  446.     SetUpCMYKRects();
  447.     SetUpBounceLine();
  448.     
  449.     theRGBColor.red = RandomInt(32767) << 1;
  450.     theRGBColor.green = RandomInt(32767) << 1;
  451.     theRGBColor.blue = RandomInt(32767) << 1;
  452.     
  453.     cmykColor.cmyk.cyan = RandomInt(32767) << 1;
  454.     cmykColor.cmyk.magenta = RandomInt(32767) << 1;
  455.     cmykColor.cmyk.yellow = RandomInt(32767) << 1;
  456.     cmykColor.cmyk.black = RandomInt(32767) << 1;
  457.     
  458.     quitting = false;
  459.     
  460.     do
  461.     {
  462.         EventLoop();
  463.     }
  464.     while (!quitting);
  465.     
  466.     if (theGWorld)
  467.         DisposeGWorld(theGWorld);
  468. }
  469.  
  470.